﻿Imports System.Data.OleDb

Public Class details
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If User.Identity.Name = "" Then
            Panel1.Visible = False
        End If
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        oleDbConn.Open()
        If Not Page.IsPostBack Then
            Try
                Dim dr As OleDbDataReader
                Dim Cmd As OleDbCommand = New OleDbCommand("SELECT Gamerz_Name FROM GamerZ Where Gamerz_ID=@f1", oleDbConn)
                Cmd.Parameters.AddWithValue("@f1", Request.QueryString("Game"))
                dr = Cmd.ExecuteReader()
                dr.Read()
                TB_Title.Text = "Re : " & dr.GetValue(0)
            Catch ex As Exception
                TB_Title.Text = ""
            End Try
        End If


        Dim dr2 As OleDbDataReader
        Dim Cmd2 As OleDbCommand = New OleDbCommand("SELECT GamerZ_ID FROM Games_Likes Where GamerZ_ID=@f1 and User_ID = @f2", oleDbConn)
        Cmd2.Parameters.AddWithValue("@f1", Request.QueryString("Game"))
        Cmd2.Parameters.AddWithValue("@f2", User.Identity.Name)
        dr2 = Cmd2.ExecuteReader()
        dr2.Read()
        If dr2.HasRows Then
            If Not IsDBNull(dr2.GetValue(0)) Then
                image_like.Visible = True
                btn_like.Visible = False
            Else
                image_like.Visible = False
                btn_like.Visible = True
            End If
        End If
    End Sub

    Protected Sub BTN_Reply_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BTN_Reply.Click

        Dim sql As String = " INSERT INTO Review (GamerZ_ID, Review_Title, Review_Reply, Review_Date, User_ID) VALUES (@f1,@f2,@f3,Now(),@f5)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@f1", Request.QueryString("Game"))
        cmd.Parameters.AddWithValue("@f2", TB_Title.Text)
        cmd.Parameters.AddWithValue("@f3", TB_Review_Reply.Text)
        cmd.Parameters.AddWithValue("@f5", User.Identity.Name)

        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("details.aspx?Game=" & Request.QueryString("Game"))

    End Sub


    Protected Sub Tag_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Tag.Click

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim Sql As String = "INSERT INTO Games_Tags (Games_ID, Tags_ID) VALUES (@f1,@f2)"
        oleDbConn.Open()

        For Each cb As ListItem In CheckBoxList1.Items
            If cb.Selected Then
                Dim cmd As OleDbCommand = New OleDbCommand(Sql, oleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", Request.QueryString("Game"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()
            End If
        Next

        If TB_tags.Text = "" Then Return

        Dim newTags = TB_tags.Text.Split(",")

        For Each newTag In newTags

            Dim tagid As Double
            Dim dr As OleDbDataReader

            Try

                Dim TagCmdNow As OleDbCommand = New OleDbCommand("SELECT ID FROM Tags Where TagName=@F1", oleDbConn)
                TagCmdNow.Parameters.AddWithValue("@f1", newTag)
                dr = TagCmdNow.ExecuteReader()
                dr.Read()
                tagid = dr.GetValue(0)
            Catch ex As Exception
                tagid = 0
            End Try



            If tagid <= 0 Then
                Dim newTagsql As String = "INSERT INTO Tags(TagName) VALUES(@f1)"
                Dim cmd As OleDbCommand = New OleDbCommand(newTagsql, oleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", newTag)
                cmd.ExecuteNonQuery()

            End If

        Next
        Response.Redirect("Tags.aspx")

    End Sub




    Protected Sub rate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles rate.Click
        Dim oleDbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim Sql As String = "DELETE FROM Games_Rate WHERE GameZ_ID=@f1 AND User_id=@f2"
        Dim Cmd As New OleDbCommand(Sql, oleDbConn)
        Cmd.Parameters.AddWithValue("@f1", Request.QueryString("Game"))
        Cmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        Cmd.ExecuteNonQuery()

        ' Insert the rating
        Dim Sqltwo As String = "INSERT INTO Games_Rate (GameZ_ID,User_id,rate) VALUES (@f1,@f2,@f3)"
        Dim Cmdtwo As New OleDbCommand(Sqltwo, oleDbConn)
        Cmdtwo.Parameters.AddWithValue("@f1", Request.QueryString("Game"))
        Cmdtwo.Parameters.AddWithValue("@f2", User.Identity.Name)
        Cmdtwo.Parameters.AddWithValue("@f3", Rating1.CurrentRating)
        Cmdtwo.ExecuteNonQuery()
    End Sub

    Protected Sub btn_like_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_like.Click

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim Cmd As OleDbCommand = New OleDbCommand("Insert into Games_likes (GamerZ_ID, User_ID) values (@f1, @f2) ", oleDbConn)
        Cmd.Parameters.AddWithValue("@f1", Request.QueryString("Game"))
        Cmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        Cmd.ExecuteNonQuery()

        image_like.Visible = True
        btn_like.Visible = False

        oleDbConn.Close()
        Cmd.Dispose()
        oleDbConn.Dispose()
    End Sub
End Class